package com.zy.je.common.core.web.rest;

import com.zy.je.common.core.utils.lang.ObjectUtil;
import com.zy.je.common.core.model.ICodeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zy
 */
public enum ResultStatus implements ICodeMessage<Integer> {
	OK(200, "OK"),
//	HTTP_ERROR_100(100, "1XX错误"),
//	HTTP_ERROR_300(300, "3XX错误"),
//	HTTP_ERROR_400(400, "4XX错误"),
//	HTTP_ERROR_500(500, "5XX错误"),

	SIGN_ERROR(120, "签名错误"),
	TIME_OUT(130, "访问超时"),
	KICK_OUT(300, "您已经在其他地方登录，请重新登录！"),
	BAD_REQUEST(407, "参数解析失败"),
	INVALID_TOKEN(401, "无效的授权码"),
	INVALID_CLIENTID(402, "无效的密钥"),
	FORBIDDEN(403, "无权限访问"),
	REQUEST_NOT_FOUND(404, "访问地址不存在！"),
	METHOD_NOT_ALLOWED(405, "不支持当前请求方法"),
	REPEAT_REQUEST_NOT_ALLOWED(406, "请求重复提交"),
	SYSTEM_ERR(501, "操作失败"),//服务器运行异常

	PARAM_REQUIRED(502, "参数为空"),
	PARAM_FORMAT_ILLEGAL(503, "参数格式错误"),
	RECORD_NOT_EXIST(504, "记录不存在"),
	RECORD_EXISTED(505, "记录已存在"),
	RECORD_ILLEGAL_STATE(506, "数据异常"),
	DICT_TRANSLATE_ERROR(507, "字典翻译出错"),

	CALL_INNER_ERROR(600, "内部服务调用异常"),


	PARAM_TRANSLATE_EMPTY(705, "翻译参数信息为空"),
	PARAM_TRANSLATE_DICT_CODE_EMPTY(706, "翻译字典参数为空"),
	PARAM_TRANSLATE_VALUE_SRC_EMPTY(707, "翻译值来源字段名为空"),

	UNSUPPORTED_GRANT_TYPE(10000, "无效授权类型"),
	USER_OR_ERROR_PWD(10002, "用户名或密码错误"),
	NOT_EXIST_USER(10003, "用户不存在"),
	CLIENT_NOT_EXIST(10004, "客户端不存在"),
	CLIENT_ERROR(10005, "客户端信息错误"),
	NOT_PARAM_USER_OR_ERROR_PWD(10006, "用户名或密码为空"),
	UNAUTH_ERROR(10007, "您没有该权限"),
	LOST_TOKEN(10008, "缺少授权码"),
	CHECK_TOKEN_ERROR(10009, "授权码校验失败"),
	EXPIRED_TOKEN(10010, "授权码过期"),
	REDIS_ERROR(1101, "redis异常"),
	REDIS_CONNECT_ERROR(1102, "redis连接异常"),
	DB_ERROR(1103, "数据库异常"),
	BUSINESS_ERROR(30000, "业务异常"),
	NULL_FILE(30010, "文件为空"),

	CALL_TPAPI_ERROR(40020, "调用外部服务失败"),
	;

	private Integer code;
	private String msg;

	private ResultStatus(final int code, final String msg) {
		this.code = code;
		this.msg = msg;
	}

	@Override
	public Integer getCode() {
		return this.code;
	}

	@Override
	public String getMessage() {
		return getMsg();
	}

	public void setCode(final int code) {
		this.code = code;
	}

	public String getMsg() {
		return this.msg;
	}

	public void setMsg(final String msg) {
		this.msg = msg;
	}

	private final static Logger LOGGER = LoggerFactory.getLogger(ResultStatus.class);

	public static ResultStatus getInstance(Integer code) {
		for (ResultStatus it : ResultStatus.values()) {
			if (it.getCode().equals(code)) {
				return it;
			}
		}
		LOGGER.error("ResultStatus(code=" + ObjectUtil.toString(code) + ") not exists");
		return null;
	}
}
